
TimeTrackingCtrl.$inject = ['$scope', '$location',
                            'TimeTrackingGetAll', 'TimeTrackingSave', 'TimeTrackingGetMonthByUser'];

function TimeTrackingCtrl($scope,  $location, TimeTrackingGetAll, TimeTrackingSave, TimeTrackingGetMonthByUser) {
    
  $scope.action = "new" ; //"new" or "update"

  $scope.dateOptions = {
        changeYear: false,
        changeMonth: false,
        yearRange: '2013:-0',
        format: 'DD, d MM, yy'
    };

  $scope.aDate = new Date()


  // Get Last TimeTrack
    TimeTrackingGetAll.get( { },  function(data) {
       if (data.result.status == "ok")
       {
          
          $scope.task = data.task;
          $scope.tags = data.tags;
          $scope.selectedTag = data.tags[0]
        console.log (":::>",data)
       }
    });


  // TASK LIST
  $scope.filterTrackingOptions = {
      filterText: ''
  };

 
  $scope.$watch('searchTaskModel', function(newText){
        $scope.filterTrackingOptions.filterText = newText;
  })
  
  $scope.gridTimeTrackingOptions = { data: 'task', 
     selectedItems: $scope.taskSelections,
     multiSelect: false,
     filterOptions: $scope.filterTrackingOptions,
     columnDefs: [{field:'dateLabel', displayName:'Date', width: 100},
                  {field:'label', displayName:'Task'}, 
                  {field:'tagLabel', displayName:'Tag', width: 100}, 
                  {field:'minutes', displayName:'Minutes', width: 70}, 
                  ],

     afterSelectionChange: function(rowItem, event) {

        $scope.action = "update"
        $scope.tagRow = rowItem
        $scope.taskTitle = rowItem.entity.label
        $scope.taskMinutes = rowItem.entity.minutes
        $scope.aDate = new Date(rowItem.entity.date)

        for (var i = 0; i < $scope.tags.length; i++) {
          if ($scope.tags[i].id == rowItem.entity.tagId) {
            $scope.selectedTag = $scope.tags[i]
            break;
          }
        };
     }/*,
     ngGridEventData: function() {
        console.log ("ngGridEventData")
     } */
  };



  $scope.createNew = function () {
    
    if ($scope.tagRow == undefined) return;

    $scope.gridTimeTrackingOptions.selectRow($scope.tagRow.rowIndex, false);

    $scope.action = "new"
    $scope.taskTitle = ""
    $scope.taskMinutes = ""
    $scope.aDate = new Date()
    $scope.selectedTag = $scope.tags[0]
    

  }

  $scope.save = function () {
   
      
  /*  console.log ("userId :::>",userId)
    console.log ("monthIndex :::>",userId)
    console.log ("year :::>",userId)
    console.log ("userId :::>",userId)
    console.log ("isArray :::>",userId)*/

    console.log ("id :::>",$scope.tagRow.entity.id)
    console.log ("action :::>",$scope.action)
    console.log ("label :::>",$scope.taskTitle)
    console.log ("date :::>",$scope.aDate)
    console.log ("minutes :::>",$scope.taskMinutes)
    console.log ("tagId :::>", $scope.selectedTag.id)

    var params = {};
    params.id = $scope.tagRow.entity.id;
    params.action = $scope.action;
    params.label = $scope.taskTitle;
    params.date = $scope.aDate;
    params.minutes = $scope.taskMinutes;
    params.tagId = $scope.selectedTag.id;

    TimeTrackingSave.get( params,  function(data) {
       if (data.result.status == "ok")
       {
         
        console.log (":::>",data)
       }
    });


  }

}
